package com.tencent.miniproject.presenter;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.tencent.miniproject.been.Post;
import com.tencent.miniproject.been.PostSummary;
import com.tencent.miniproject.constants.Constant;
import com.tencent.miniproject.fragment.i.IUserBillFragment;
import com.tencent.miniproject.presenter.i.IUserBillPresenter;
import com.tencent.miniproject.utils.GsonUtil;
import com.tencent.miniproject.utils.OkHttpResultCallback;
import com.tencent.miniproject.utils.OkHttpUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class UserBillPresenter implements IUserBillPresenter {

    private IUserBillFragment mView;    //view
    private PostSummary mPostSummary;   //我的页面总的数据缓存

    public UserBillPresenter(IUserBillFragment fragment) {
        super();
        mView = fragment;
    }

    /**
     * 获取我的页面——内容数据
     */
    @Override
    public void getPostSummaryDataFromNet() {
//        Map<String, String> map = new HashMap<>();
//        map.put("id", "2");
        OkHttpUtil.getInstance().getAsync(Constant.SERVER_URL + "/post/getMyPosts", new OkHttpResultCallback() {
            @Override
            public void onError(Call call, Exception e) {
                mView.showToastInFragment("ssd");
            }

            @Override
            public void onResponse(byte[] bytes) {
                try {
                    Log.i("ning", "onResponse222: " + new String(bytes));
                    String string = new String(bytes);
                    mPostSummary = GsonUtil.gsonToBean(string, PostSummary.class);
                    Log.i("ning", "onResponse: " + mPostSummary.getMessage().getHeadUrl());
                    //获取数据之后通知view层从presenter拿数据
                    mView.notifyDataChanged();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 获取别人的账单页面——内容数据
     * @param userId userId
     */
    @Override
    public void getPostSummaryDataFromNet(int userId) {
        OkHttpUtil.getInstance().getAsync(Constant.SERVER_URL + "/post/getPostsByUser?id=" + userId, new OkHttpResultCallback() {
            @Override
            public void onError(Call call, Exception e) {
                mView.showToastInFragment("ssd");
            }

            @Override
            public void onResponse(byte[] bytes) {
                Log.i("ning", "onResponse222: " + new String(bytes));
                String string = new String(bytes);
                mPostSummary = GsonUtil.gsonToBean(string, PostSummary.class);
                Log.i("ning", "onResponse: " + mPostSummary.getMessage().getHeadUrl());
                //获取数据之后通知view层从presenter拿数据
                mView.notifyDataChanged();
            }
        });
    }

    @Override
    public List<Post> getPostList() {
        if (mPostSummary == null){
            return new ArrayList<>();
        }
        return mPostSummary.getMessage().getPosts();
    }

    @Override
    public String getUserImgUrl() {
        return mPostSummary.getMessage().getHeadUrl();
    }

    @Override
    public String getUserName() {
        return mPostSummary.getMessage().getUsername();
    }

    @Override
    public long getSpendMoney() {
        return mPostSummary.getMessage().getCost();
    }
}
